<template>
	<view class="content">
		<view class="card" style="position: relative;">
			<text class="card-title">课程标题</text>
			<textarea maxlength="20"
				placeholder="请输入课程标题"
				:auto-height="true"
				v-model="_form.name"
				class="textarea-content"></textarea>
			<text class="textarea-content-count">{{_form.name.length}}/20</text>
		</view>
		<view class="card">
			<text class="card-title">课程分类</text>
			<uni-data-picker v-model="typesIndex" :localdata="types" :clear-icon="false"></uni-data-picker>
		</view>
		<view class="card flex flex-col">
			<text class="card-title">上传封面(必填)</text>
			<view class="images-box">
				<view class="image-box" @click="SelectImage()"></view>
				<view class="image-box" v-if="_form.headimg">
					<image src="" mode="aspectFill"></image>
					<uni-icons @click="DeleteImage()" type="close" color="#fff" class="delete-image"></uni-icons>
				</view>
			</view>
		</view>
		<view class="card" style="position: relative;">
			<text class="card-title">课程描述</text>
			<textarea maxlength="500"
				placeholder="请描述您的内容"
				:auto-height="true"
				v-model="_form.brief"
				class="textarea-content"></textarea>
			<text class="textarea-content-count">{{_form.brief.length}}/500</text>
		</view>
		<view class="card" style="position: relative;">
			<text class="card-title">课程介绍</text>
			<textarea maxlength="500"
				placeholder="请介绍您的内容"
				:auto-height="true"
				v-model="_form.describe"
				class="textarea-content"></textarea>
			<text class="textarea-content-count">{{_form.describe.length}}/500</text>
		</view>
		<view class="card">
			<text class="card-title">课程类型</text>
			<uni-data-picker v-model="coustsIndex" :localdata="cousts" :clear-icon="false"></uni-data-picker>
		</view>
		<view v-if="coustsIndex == 0" class="card" style="position: relative;">
			<text class="card-title">课程内容</text>
			<textarea maxlength="-1"
				placeholder="请输入课程内容"
				:auto-height="true"
				v-model="_form.textContent"
				class="textarea-content"></textarea>
			<text class="textarea-content-count">{{_form.textContent.length}}</text>
		</view>
		<view v-else-if="coustsIndex == 1" class="card flex flex-col">
			<text class="card-title">添加音频</text>
			<view class="images-box">
				<view class="image-box" @click="SelectFile('audio')"></view>
				<view v-if="_form.audio" class="image-box" style="background: #e4f2ff;">
					<image src="" mode="aspectFill"></image>
					<uni-icons @click="DeleteFile('audio')" type="close" color="#fff" class="delete-image"></uni-icons>
				</view>
			</view>
		</view>
		<view v-else class="card flex flex-col">
			<text class="card-title">添加视频</text>
			<view class="images-box">
				<view class="image-box" @click="SelectFile('video')"></view>
				<view v-if="_form.video" class="image-box" style="background: #def9f2;">
					<image src="" mode="aspectFill"></image>
					<uni-icons @click="DeleteFile('video')" type="close" color="#fff" class="delete-image"></uni-icons>
				</view>
			</view>
		</view>
		<view class="card">
			<text class="card-title">红钻价格</text>
			<uni-easyinput v-model="_form.redprice" type="number" placeholder="请输入红钻价格"></uni-easyinput>
		</view>
		<view class="card">
			<text class="card-title">蓝钻价格</text>
			<uni-easyinput v-model="_form.blueprice" type="number" placeholder="请输入蓝钻价格"></uni-easyinput>
		</view>
		<view @click="Submit" class="confirm-button flex items-center justify-center">提交</view>
	</view>
</template>

<script setup>
	import { ref, onMounted } from 'vue'
	import { onLoad } from '@dcloudio/uni-app'
	import config from '../../config/index.js'
	import aboutUpload from '../../apis/apiList/indexApi.js'
	
	let AnchorId = ''
	const types = ref([])
	const typesIndex = ref(0)
	const cousts = ref([
		{
			text: '文本',
			value: 0
		},
		{
			text: '语音',
			value: 1
		},
		{
			text: '视频',
			value: 2
		}
	])
	const coustsIndex = ref(0)
	const _form = ref({
		name: '',
		headimg: '',
		brief: '',
		describe: '',
		textContent: '',
		audio: '',
		video: '',
		redprice: '',
		blueprice: ''
	})
	
	onLoad(_options => {
		AnchorId = _options.id
	})
	
	onMounted(() => {
		GetTypes()
	})
	
	/**
	 * @description 获取课程类型
	 */
	function GetTypes() {
		uni.request({
			url: config.baseUrl + '/app/getCouresTypelist',
			method: 'POST'
		}).then(res => {
			let { data } = res
			types.value = data.data.map(item => {
				item.text = item.name
				item.value = item.id
				return item
			})
		})
	}
	
	/**
	 * @description 选择图片
	 */
	function SelectImage() {
		uni.chooseImage({
			count: 1,
			sizeType: ['original', 'compressed'],
			sourceType: ['album', 'camera'],
			success: function (res) {
				_form.value.headimg = res.tempFilePaths[0]
			}
		})
	}
	
	/**
	 * @description 删除图片
	 */
	function DeleteImage() {
		_form.value.headimg = ''
	}
	
	/**
	 * @description 选择文件
	 */
	function SelectFile(type) {
		type == 'video' && uni.chooseVideo({
			count: 1,
			sizeType: ['original', 'compressed'],
			sourceType: ['album', 'camera'],
			success: res => {
				_form.value.video = res.tempFilePath
			}
		})
		type != 'video' && plus.io.chooseFile({
			title: '请选择音频文件',
			multiple: false,
			filter: 'audio'
		}, res => {
			let path = res.files[0]
			if (path.startsWith('/storage')) {
				path = `file://${path}`
			}
			_form.value.audio = path
		})
	}
	
	/**
	 * @description 删除文件
	 */
	function DeleteFile(type) {
		_form.value[type] = ''
	}
	
	/**
	 * @description 提交
	 */
	async function Submit() {
		if (!_form.value.name) {
			uni.showToast({
				title: '请输入课程名称',
				icon: 'none'
			})
			return
		}
		if (!typesIndex.value) {
			uni.showToast({
				title: '请选择课程分类',
				icon: 'none'
			})
			return
		}
		if (!_form.value.headimg) {
			uni.showToast({
				title: '请上传封面',
				icon: 'none'
			})
			return
		}
		if (!_form.value.brief) {
			uni.showToast({
				title: '请输入课程描述',
				icon: 'none'
			})
			return
		}
		if (!_form.value.describe) {
			uni.showToast({
				title: '请输入课程介绍',
				icon: 'none'
			})
			return
		}
		if (coustsIndex.value == 0 && !_form.value.textContent) {
			uni.showToast({
				title: '请输入课程内容',
				icon: 'none'
			})
			return
		}
		if (coustsIndex.value == 1 && !_form.value.audio) {
			uni.showToast({
				title: '请上传音频',
				icon: 'none'
			})
			return
		}
		if (coustsIndex.value == 2 && !_form.value.video) {
			uni.showToast({
				title: '请上传视频',
				icon: 'none'
			})
			return
		}
		if (!_form.value.redprice) {
			uni.showToast({
				title: '请输入红钻价格',
				icon: 'none'
			})
			return
		}
		if (!_form.value.blueprice) {
			uni.showToast({
				title: '请输入蓝钻价格',
				icon: 'none'
			})
			return
		}
		let params = {
			'anchor.id': AnchorId,
			'coursetype.id': typesIndex.value,
			name: _form.value.name,
			headimg: _form.value.headimg,
			brief: _form.value.brief,
			describe: _form.value.describe,
			cousts: coustsIndex.value,
			redprice: _form.value.redprice,
			blueprice: _form.value.blueprice,
			dur: ''
		}
		if (coustsIndex.value == 0) {
			params.content = _form.value.textContent
		} else if (coustsIndex.value == 1) {
			let res = await aboutUpload.upLoadImage({
				filePath: _form.value.audio,
				name: 'file'
			})
			params.content = res.data.url
		} else if (coustsIndex.value == 2) {
			let res = await aboutUpload.upLoadImage({
				filePath: _form.value.video,
				name: 'file'
			})
			params.content = res.data.url
		}
		uni.request({
			url: config.baseUrl + '/app/anchorReleaseCourse',
			header: {
				"Content-Type": "application/x-www-form-urlencoded"
			},
			method: 'POST',
			data: params
		}).then(res => {
			let { data } = res
			let { code, msg } = data
			if (code == 200) {
				uni.showToast({
					title: '发布成功',
					icon: 'none'
				})
				setTimeout(() => {
					uni['my-lesson.ReloadList'] && uni['my-lesson.ReloadList']()
					uni.navigateBack()
				}, 1000)
			} else {
				uni.showToast({
					title: msg,
					icon: 'none'
				})
			}
		})
	}
</script>

<style>
	page {
		background-image: none;
	}
	
	.content {
		padding: 20rpx 20rpx 60rpx;
		display: flex;
		flex-direction: column;
		gap: 20rpx;
	}
	
	.card {
		background: #fff;
		border-radius: 20rpx;
		padding: 20rpx;
		display: flex;
		flex-direction: column;
		gap: 20rpx;
	}
	
	.card-title {
		font-size: 28rpx;
		font-weight: bold;
	}
	
	.textarea-content {
		border-radius: 10rpx;
		background: #f2f7fa;
		font-size: 26rpx;
		width: calc(100vw - 20rpx * 6);
		min-height: 150rpx;
		padding: 20rpx 20rpx 50rpx;
	}
	
	.textarea-content-count {
		position: absolute;
		bottom: 30rpx;
		right: 34rpx;
		font-size: 24rpx;
		color: #bcbfbf;
	}
	
	.images-box {
		display: flex;
		gap: 20rpx;
		flex-wrap: wrap;
	}
	
	.image-box {
		width: 130rpx;
		height: 130rpx;
		border-radius: 14rpx;
		overflow: hidden;
		position: relative;
		background: #f5f7fa;
	}
	
	.image-box image {
		width: 100%;
		height: 100%;
	}
	
	.image-box:nth-child(1)::before {
		content: '';
		width: 2px;
		height: 60%;
		background: #b2c0d0;
		position: absolute;
		top: 50%;
		left: 50%;
		transform: translate(-50%, -50%);
	}
	
	.image-box:nth-child(1)::after {
		content: '';
		width: 60%;
		height: 2px;
		background: #b2c0d0;
		position: absolute;
		top: 50%;
		left: 50%;
		transform: translate(-50%, -50%);
	}
	
	.delete-image {
		position: absolute;
		top: 0;
		right: 0;
		background: rgba(0, 0, 0, .5);
		padding: 2rpx;
		border-radius: 0 14rpx 0 14rpx;
	}
	
	.confirm-button {
		font-size: 30rpx;
		font-weight: bold;
		color: #fff;
		background: #319eff;
		height: 80rpx;
		border-radius: 40rpx;
	}
</style>
